/**
 * 页码工具类型
 */
enum PageToolType {
    normal, showMaxi
}

/**
 * 页码工具
 * @Author Ace.c
 * @Create 2016-10-30 14:10
 */
class PageTool extends BaseSprite {
    private labelDisplay: eui.Label;
    public leftBtnDisplay: eui.Button;
    public rightBtnDisplay: eui.Button;

    type: PageToolType = PageToolType.showMaxi;
    mini: number = 1;
    maxi: number = 1;
    page: number = 1;

    constructor() {
        super();

        this.skinName = "skins.PageToolSkin";
    }

    childrenCreated() {
        super.childrenCreated();

        this.leftBtnDisplay.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.rightBtnDisplay.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
    }

    private clickHandler(e: egret.TouchEvent) {
        if (!game.isBtnDisabledByTime) game.isBtnDisabledByTime = true;
        else return;

        switch (e.currentTarget) {
            case this.rightBtnDisplay:
                if (this.page < this.maxi) {
                    this.page++;
                    this.update();
                }
                break;
            case this.leftBtnDisplay:
                if (this.page > this.mini) {
                    this.page--;
                    this.update();
                }
                break;
        }

        this.dispatchEventWith(CommonEventType.CHANGED);
    }

    private update() {
        this.page = this.page < this.mini ? this.mini : this.page;
        this.page = this.page > this.maxi ? this.maxi : this.page;

        switch (this.type) {
            case PageToolType.normal:
                this.labelDisplay.text = "" + this.page;
                break;
            case PageToolType.showMaxi:
                this.labelDisplay.text = "" + this.page + "/" + this.maxi;
                break;
        }
    }

    setScope(mini: number = 1, maxi: number = 1) {
        maxi = maxi ? maxi : 1;
        maxi = maxi < mini ? mini : maxi;

        this.mini = mini;
        this.maxi = maxi;

        this.update();
    }

    setPage(value: number) {
        this.page = value;

        this.update();
    }

    setMaxChars(value: number) {
        this.labelDisplay.maxChars = value;
    }
}